package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.user.dao.entity.*;
import com.niiwoo.civet.user.dao.mapper.*;

import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户登录账号本地服务
 */
@Service
public class UserLoginAccountService implements LoginAccountRedisService {

    @Autowired
    LoginAccountMappingMapper loginAccountMappingMapper;

    @Autowired
    UserAccountMapper userAccountMapper;

    @Autowired
    UserBasicInfoMapperExt ucbasicInfoMapper;

    @Autowired
    UserExtInfoMapperExt userExtInfoMapper;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    InviteRegisterMapper inviteRegisterMapper;

    @Autowired
    UserAuthorizationInfoMapperExt userAuthorizationInfoMapperExt;

    @Autowired
    UserNiiwooScoreMapperExt userNiiwooScoreMapperExt;
    @Autowired
    FltFundChannelMapper fltFundChannelMapper;
    @Autowired
    FltFundChannelMapperExt fltFundChannelMapperExt;

    /**
     * 创建账号并初始化基础信息
     *
     * @param accountMapping 账号映射
     * @param account 账号
     * @param basicInfo 基础信息
     * @param extInfo 扩展信息
     * @param inviteRegisterOpt 邀请关系
     * @param authorizationInfo 认证信息
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean initAccountAndBasicInfo(LoginAccountMapping accountMapping, UcAccount account, UserBasicInfo basicInfo, UserExtInfo extInfo,
                                           Optional<InviteRegister> inviteRegisterOpt, UserAuthorizationInfo authorizationInfo, NiiwooScore score) {
        return loginAccountMappingMapper.insertSelective(accountMapping) > 0 &&
                userAccountMapper.insertSelective(account) > 0 &&
                ucbasicInfoMapper.insertSelective(basicInfo) > 0 &&
                userExtInfoMapper.insertSelective(extInfo) > 0 &&
                (!inviteRegisterOpt.isPresent() || inviteRegisterMapper.insertSelective(inviteRegisterOpt.get()) > 0) &&
                userAuthorizationInfoMapperExt.insertSelective(authorizationInfo) > 0 &&
                userNiiwooScoreMapperExt.insertSelective(score) > 0;
    }

    /**
     * 用户是否存在
     *
     * @param accountName 登录名
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean isExists(String accountName) {
        return getAccountMapping(accountName) != null;
    }

    /**
     * 查找账户映射
     *
     * @param accountName 登录名
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public LoginAccountMapping getAccountMapping(String accountName) {
        return loginAccountMappingMapper.selectByPrimaryKey(accountName);
    }

    /**
     * 根据userId查询账号
     */
    public UcAccount getAccountByUserId(String userId) {
        return userAccountMapper.selectByPrimaryKey(userId);
    }

    /**
     * 更新用户登录信息
     *
     * @param extInfo 扩展信息
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean updateLoginInfo(UserExtInfo extInfo) {
        return userExtInfoMapper.updateByPrimaryKeySelective(extInfo)> 0;
    }

    @Override
    public RedisTemplate<String, String> getStringRedisTemplate() {
        return this.redisTemplate;
    }

    /**
     * 修改登录账号
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean updateLoginAccountInfo(UcAccount account) {
        return userAccountMapper.updateByPrimaryKeySelective(account) > 0;
    }

    /**
     * 资产端用户创建账号并初始化基础信息
     * @param accountMapping
     * @param account
     * @param basicInfo
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean initAccountAndBasicInfo(LoginAccountMapping accountMapping, UcAccount account, UserBasicInfo basicInfo, UserAuthorizationInfo authorizationInfo,NiiwooScore score,UserExtInfo extInfo) {
        return loginAccountMappingMapper.insertSelective(accountMapping) > 0 &&
                userAccountMapper.insertSelective(account) > 0 &&
                ucbasicInfoMapper.insertSelective(basicInfo) > 0 &&
                userExtInfoMapper.insertSelective(extInfo) > 0 &&
                userAuthorizationInfoMapperExt.insertSelective(authorizationInfo) > 0 &&
                userNiiwooScoreMapperExt.insertSelective(score) > 0;
    }

    public UserExtInfo getAccountExtInfo(String userId) {
        return userExtInfoMapper.selectByPrimaryKey(userId);
    }

    /**
     * 初始化返利投用户信息
     * @param fltFundChannel
     * @return
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public boolean initFltUserInfo(FltFundChannel fltFundChannel){
        return fltFundChannelMapper.insertSelective(fltFundChannel) > 0 ;
    }
    /**
     * 根据ID查询用户
     */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public UserBasicInfo selectUserBasicInfo(String userId) {

        return ucbasicInfoMapper.selectByPrimaryKey(userId);
    }

    /**
     * 根据userId查询返利投注册关系表
     * @param userId
     * @return
     */
    public FltFundChannel selectFltFundUserInfo(String userId){
        return fltFundChannelMapper.selectByPrimaryKey(userId);
    }

    /**
     * 获取用户信息
     * @param uid
     * @param registertoken
     * @return
     */
    public FltFundChannel selectFltFundByUidAndToken(String uid,String registertoken){
        return fltFundChannelMapperExt.selectByUidAndRegiToken(uid,registertoken);
    }

}
